package com.ie.service.admin;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.common.security.utils.ConfigrarionBean;
import com.ie.common.security.utils.JwtTokenUtil;
import com.ie.common.security.utils.RedisUtil;
import com.ie.entity.AUserEntity;
import com.ie.mapper.admin.AUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ie.common.security.utils.MD5Util;

import java.util.*;

@Service
public class AUserServiceImpl extends ServiceImpl<AUserMapper, AUserEntity> implements AUserService {
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ConfigrarionBean config;
    // 对传入的对象参数进行比较
    // 1.有这个用户:a.密码正确;b.密码错误
    // 无此用户:返回
    // 2.密码正确:返回token,跳转到相应展示页面
    @Override
    public ResponseResult<Map<String, String>> login(AUserEntity aUserEntity) {
// 获取到这个参数
        Long count = lambdaQuery().eq(AUserEntity::getAname, aUserEntity.getAname()).count();
        if (count == 0) {
            return ResponseResult.getMessageResult(null, "A001");
        }
        HashMap<String, String> mapuser = new HashMap<>();
        mapuser.put("aname", aUserEntity.getAname());
        mapuser.put("apwd", aUserEntity.getApwd());
        List<AUserEntity> userlist = query().allEq(mapuser).list();
        if (userlist.size() > 0) {
            String token = jwtTokenUtil.createToken(aUserEntity.getAname());
            redisUtil.set("login_" + aUserEntity.getAname(), aUserEntity.getAname(), config.getRedisExpiration());
            // 把生成的有效信息返回给ResponseResultl
            HashMap<String, String> loginresult = new HashMap<>();
            loginresult.put("authtoken", token);
            loginresult.put("aname", aUserEntity.getAname());
            loginresult.put("aid", aUserEntity.getId() + "");
            return ResponseResult.getSuccessResult(loginresult);
        } else {
            return ResponseResult.getMessageResult(null, "A002");
        }
    }

    @Override
    public ResponseResult<String> changePassword(AUserEntity aUserEntity) {

        if (aUserEntity == null || aUserEntity.getAname() == null ||
                aUserEntity.getOldPwd() == null || aUserEntity.getNewPwd() == null) {
            return ResponseResult.getMessageResult(null, "A000");
        }



        AUserEntity user = lambdaQuery()
                .eq(AUserEntity::getAname, aUserEntity.getAname())
                .one();
        if (user == null) {
            return ResponseResult.getMessageResult(null, "A001");
        }

        if (!user.getApwd().equals(MD5Util.MD5(aUserEntity.getOldPwd()))) {
            return ResponseResult.getMessageResult(null, "A002");
        }

        if (user.getApwd().equals(MD5Util.MD5(aUserEntity.getNewPwd()))) {
            return ResponseResult.getMessageResult(null, "A003");
        }

        user.setApwd(MD5Util.MD5(aUserEntity.getNewPwd()));
        boolean success = updateById(user);

        if (success) {
            return ResponseResult.getMessageResult(null, "A004");
        } else {
            return ResponseResult.getMessageResult(null, "A005");
        }


    }





}
